package com.godz.getter.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.godz.getter.mapper.GoodsMapper;
import com.godz.getter.mapper.PCateMapper;
import com.godz.getter.pojo.Goods;
import com.godz.getter.pojo.ProCategory;
import com.godz.getter.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class IndexController {
    /**
     * 字段注入的缺点
     * 只需要使用 @Autowired 就很容易的进行依赖注入，为什么说这是个缺点呢，因为这种方便可能让你放弃对依赖的思考，结果就是你的类的依赖可能是十几个或者更多，这样违反了SPR.
     * 对单元测试不友好，你没办法直接初始化这个类，必须依赖 DI 容器.
     * 类的依赖被隐藏起来，并不能像构造函数那样在初始化时，就直观的知道这个类有哪些依赖.
     * 你的类跟DI容器强耦合在一起.
     */
    //自己定义SQL
    @Autowired
    GoodsService goodsService;
    //Mybatis-Plus
    @Autowired
    GoodsMapper goodsMapper;
    //产品种类 完全无视
    @Autowired
    PCateMapper pCateMapper;

    @RequestMapping(value = {"/", "/index"})
    public String index(Model model, @RequestParam(value = "pageNow", defaultValue = "1") int pageNow) {
        //按时间取前九条数据
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("g_date");
        wrapper.eq("pass", 1);
        Page<Goods> goodsPage = new Page<>(pageNow, 9);
        Page<Goods> goodsPage2 = goodsMapper.selectPage(goodsPage, wrapper);
        List<Goods> goods = goodsPage2.getRecords();
        model.addAttribute("Goods", goods);
        //查询总共多少页数据
        QueryWrapper<Goods> queryWrapper = new QueryWrapper();
        queryWrapper.eq("pass", "1");
        Integer count = goodsMapper.selectCount(queryWrapper);
        //总页数
        int pageAll = (int) Math.floor(count / 9);
        if (count % 9 != 0) {
            pageAll++;
        }
        model.addAttribute("pageNow", pageNow);
        model.addAttribute("pageAll", pageAll);
        //可以选择的种类
        List<ProCategory> categories = pCateMapper.selectList(null);
        model.addAttribute("Categories", categories);
        return "index";
    }

    //点击不同产品
    @RequestMapping("/index2")
    public String index2(Model model, @RequestParam(value = "type") int type) {
        //按时间取前九条数据
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("g_date");
        wrapper.eq("pass", 1);
        wrapper.eq("g_category", type);
        wrapper.last("limit 9");
        List<Goods> goods = goodsMapper.selectList(wrapper);
        model.addAttribute("Goods", goods);
        model.addAttribute("pageNow", 1);
        model.addAttribute("pageAll", 1);
        //可以选择的种类
        List<ProCategory> categories = pCateMapper.selectList(null);
        model.addAttribute("Categories", categories);
        return "index";
    }

    //点击搜索
    @PostMapping("/index3")
    public String index2(Model model, @RequestParam(value = "text") String name) {
        //按时间取前九条数据
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("g_date");
        wrapper.eq("pass", 1);
        wrapper.like("g_name", name);//根据商品名称
        wrapper.or().eq("g_stu_id", name);//根据学号
        wrapper.last("limit 9");
        List<Goods> goods = goodsMapper.selectList(wrapper);

        model.addAttribute("Goods", goods);
        model.addAttribute("pageNow", 1);
        model.addAttribute("pageAll", 1);
        //可以选择的种类
        List<ProCategory> categories = pCateMapper.selectList(null);
        model.addAttribute("Categories", categories);
        return "index";
    }


}
